package zt.song.config;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/11/17
 * @Version 1.0
 **/
@Component
public class MyAuthericFaileHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest reuest, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //设置响应的状态码：403
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        //设置响应头
        response.setHeader("Content-Type","application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("{\"status\":\"error\",\"msg\":\"对不起，您权限不足\"  }");
        writer.close();
    }
}
